﻿
qx.ui.table.TablePane.USE_TABLE = true;
qx.ui.table.TablePane.USE_ARRAY_JOIN = true;

var flashStrA = "<div style='width:100%;height:100%'><OBJECT classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/ pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" WIDTH=\"565\" HEIGHT=\"420\" id=\"FC2Column\" ALIGN=\"\"> <PARAM NAME=movie VALUE=\"FC2Column.swf?IsLocal=1&dataXML=";
var flashStrB = "\"> <PARAM NAME=quality VALUE=high> <PARAM NAME=bgcolor VALUE=#FFFFFF> <EMBED src=\"FC2Column2D.swf?IsLocal=1&dataXML=";
var flashStrC = "\" quality=high bgcolor=#FFFFFF WIDTH=\"565\" HEIGHT=\"420\" NAME=\"FC2Column\" ALIGN=\"\" TYPE=\"application/x-shockwave-flash\" PLUGINSPAGE=\"http://www.macromedia.com/go/getflashplayer\"></EMBED> </OBJECT></div>";
var currentChartType = "FC2Pie3D.swf";//当前选中的是那种图形
var currentChartType2 = "FC2MSColumn.swf";//当前选中的是那种图形
/**
 * 显示某个图表。
 */
function buildChart(chartSetting){
	if(chartSetting.isDouble){
	  buildChartDouble(chartSetting);
	}else{
	  buildChartSingal(chartSetting);
	}
}
function buildChartDouble(chartSetting){
	data = chartSetting.data;
	pane = chartSetting.pane;
	config = chartSetting.config;
			var flashStr1 = flashStrA;
			var flashStr2 = flashStrB;
			var flashStr3 = flashStrC;
			flashStr1 = flashStrA.replace("FC2Column.swf",flashPath+currentChartType2);
			flashStr1 = flashStr1.replace("FC2Column.swf",flashPath+currentChartType2);
			flashStr2 = flashStrB.replace("FC2Column.swf",flashPath+currentChartType2);
			flashStr2 = flashStr2.replace("FC2Column.swf",flashPath+currentChartType2);
			flashStr3 = flashStrC.replace("FC2Column.swf",flashPath+currentChartType2);
			flashStr3 = flashStr3.replace("FC2Column.swf",flashPath+currentChartType2);
		var strFlashForCFSXcnt = "<graph caption='"+config.caption
		+"' subCaption='"+config.subCaption+"' yaxisname='"+config.yaxisname
		+"' xaxisname='"+config.xaxisname+"' numdivlines='3' zeroPlaneColor='F23456' areaColor='FF9F6C' zeroPlaneAlpha='40' baseFontSize='18px'>";
		var colors = ["F23456","009966","FF6600","CC3399","FFCC33"];
		var value;
		var categoryStr = "<categories>";
		var datasets = [];
		for(var i = 0; i < data.length; i++){
			var m = data[i];
			var name = m.name;
			categoryStr += "<category name=\'"+name+"\' />";
			var j = 0;
			value = m.value;
			for(var j = 0; j < value.length; j++){
				var color = colors[j%colors.length];
				if(!datasets[j])datasets[j]="<dataset seriesname='Product A' color='"+color+"' showValue='1'>";
				datasets[j] += "<set value='"+value[j]+"' /> ";
			}
		}
		categoryStr += "</categories>";
		strFlashForCFSXcnt += categoryStr;
		for(var i=0; i<datasets.length; i++) {
			datasets[i] +=  "</dataset>";
			strFlashForCFSXcnt += datasets[i];
		}
		strFlashForCFSXcnt+="</graph>";
		var htmlStr = flashStr1+strFlashForCFSXcnt+flashStr2+strFlashForCFSXcnt+flashStr3;
		var htmlEmbeded = new qx.ui.embed.HtmlEmbed(htmlStr);
		pane.removeAll();
		pane.add(htmlEmbeded);	
}
function buildChartSingal(chartSetting){
	data = chartSetting.data;
	pane = chartSetting.pane;
	config = chartSetting.config;
			var flashStr1 = flashStrA;
			var flashStr2 = flashStrB;
			var flashStr3 = flashStrC;
			flashStr1 = flashStrA.replace("FC2Column.swf",flashPath+currentChartType);
			flashStr1 = flashStr1.replace("FC2Column.swf",flashPath+currentChartType);
			flashStr2 = flashStrB.replace("FC2Column.swf",flashPath+currentChartType);
			flashStr2 = flashStr2.replace("FC2Column.swf",flashPath+currentChartType);
			flashStr3 = flashStrC.replace("FC2Column.swf",flashPath+currentChartType);
			flashStr3 = flashStr3.replace("FC2Column.swf",flashPath+currentChartType);
		var strFlashForCFSXcnt = "<graph caption='"+config.caption
		+"' subCaption='"+config.subCaption+"' yaxisname='"+config.yaxisname
		+"' xaxisname='"+config.xaxisname+"' numdivlines='3' zeroPlaneColor='F23456' areaColor='FF9F6C' zeroPlaneAlpha='40' baseFontSize='18px'>";
		var colors = ["F23456","FF6600","009966","CC3399","FFCC33"];
		var value;
		for(var i = 0; i < data.length; i++){
			var m = data[i];
			var name = m.name;
			value = m.value;
			value = formatFloat(value,3);
			var color = colors[i%colors.length];
			strFlashForCFSXcnt += "<set name='"+name+"' value='"+value+"' color='"+color+"'/> ";
		}
		strFlashForCFSXcnt+="</graph>";
		var htmlStr = flashStr1+strFlashForCFSXcnt+flashStr2+strFlashForCFSXcnt+flashStr3;
		var htmlEmbeded = new qx.ui.embed.HtmlEmbed(htmlStr);
		pane.removeAll();
		pane.add(htmlEmbeded);	
}
/**
 * 页面载入后执行。
 */
function main(){
	var d = qx.ui.core.ClientDocument.getInstance();

	canvasForm = new qx.ui.layout.CanvasLayout;
	canvasForm2 = new qx.ui.layout.CanvasLayout;
	canvasForm.setLocation(0,0);
	canvasForm2.setLocation(0,0);
	//chart selection buttons
	var chartTypes = ["FC2Line.swf","FC2Pipe.swf","FC2Pie2D.swf","FC2Pie3D.swf"
						,"FC2Column2D.swf","FC2Column.swf","FC2Area2D.swf","FC2Area3D.swf"
						,"FC2Doughnut.swf"
						];
	var chartTypes2 = ["FC2MSColumn.swf","FC2MSColumn2D.swf","FC2MSLine.swf","FC2MSArea.swf"
						];
	var chartTypeNames = ["折线图","立体折线","2D饼图","3D饼图"
						,"2D柱状图","3D柱状图"," 2D区域图","3D区域图"
						,"圈状图"
						];
	var chartTypeNames2 = ["3D柱状图","2D柱状图","线状图","区域"
						];
	var topValue = 0;
	for(var i=0; i<chartTypes.length; i++) {
		var chartType = chartTypes[i];
		var btn = new qx.ui.form.Button(chartTypeNames[i]);
		btn.setUserData("chartType",chartType);
		btn.setWidth(60);
		topValue+=24;
		btn.setLocation(0,topValue);
		btn.addEventListener("execute",function(){
				currentChartType = this.getUserData("chartType");
				buildCharts();
		});
		canvasForm.add(btn);
	}
	canvasForm.setDisplay(false);
	topValue = 0;
	for(var i=0; i<chartTypes2.length; i++) {
		var chartType = chartTypes2[i];
		var btn = new qx.ui.form.Button(chartTypeNames2[i]);
		btn.setUserData("chartType",chartType);
		btn.setWidth(60);
		topValue+=24;
		btn.setLocation(0,topValue);
		btn.addEventListener("execute",function(){
				currentChartType2 = this.getUserData("chartType");
				buildCharts();
		});
		canvasForm2.add(btn);
	}
	//canvasForm2.setDisplay(false);
	var groupLayout = new qx.ui.groupbox.GroupBox(" ", "icon/32/chart.png");
	groupLayout.setDimension("100%", "100%");
	groupLayout.setLocation(5, 5);

	groupLayout.add(canvasForm);
	groupLayout.add(canvasForm2);

	var tf1 = new qx.ui.pageview.tabview.TabView;
	tf1.set({ left: 80, top: 0, right: 10, bottom: 0 });
	
	for(var i=0; i<chartSettings.length; i++) {
		var chartSetting = chartSettings[i];
		var t1_1 = new qx.ui.pageview.tabview.TabViewButton(chartSetting.config.caption);
		var p1_1 = new qx.ui.pageview.tabview.TabViewPage(t1_1);
		if(i == 0)t1_1.setChecked(true);
		tf1.getBar().add(t1_1);
		tf1.getPane().add(p1_1);
		chartSetting.pane = p1_1;
		t1_1.setUserData("chartSetting",chartSetting);
		t1_1.addEventListener("click",function(){
			var setting = this.getUserData("chartSetting");
			if(setting.isDouble){
				canvasForm2.show();
				canvasForm.setDisplay(false);
			}else{
				canvasForm.show();
				canvasForm2.setDisplay(false);
			}
		});
	}
	
	groupLayout.add(tf1);
	d.add(groupLayout);
};
/**
 * 初始化完成后执行。
 */
function finalize(){
	buildCharts();
	if(chartSettings[0].isDouble){
		canvasForm.setDisplay(false);
		canvasForm2.show();
	}else{
		canvasForm2.setDisplay(false);
		canvasForm.show();
	}
};
function buildCharts(){

	for(var i=0; i<chartSettings.length; i++) {
		buildChart(chartSettings[i]);
	}
}
qx.core.Init.getInstance().defineMain(main);
qx.core.Init.getInstance().defineFinalize(finalize);
function formatFloat(f,decimal){
	if(decimal == 0)return parseInt(f+0.5);
	if(!decimal) decimal = 2;
	var t = 1;
	for(var i = 0; i < decimal; i++){
		f*=10.0;
		t*=10;
	}
f+=0.5;
f = parseInt(f);
f = 1.0*f/t;
  return ""+f;
}